package com.xyht.medicine_management_system.modules.stock.controller;


import com.xyht.medicine_management_system.auth.util.JwtTokenUtil;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.medicine_management_system.modules.medicine.entity.req.MmsMedicineExportReq;
import com.xyht.medicine_management_system.modules.stock.entity.req.*;
import com.xyht.medicine_management_system.modules.stock.service.MmsMedicineStockService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 药品库存 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-11-30
 */
@RestController
@SecretBody
@RequestMapping("/stock")
public class MmsMedicineStockController {

    @Resource
    private MmsMedicineStockService medicineStockService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty("添加库存")
    @PostMapping("/add")
    public ResponseResult addMedicineStock(@RequestBody MmsMedicineStockAddReq addReq, HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return medicineStockService.addMedicineStock(addReq,userId);
    }

    @ApiModelProperty("修改库存")
    @PostMapping("/update")
    public ResponseResult updateMedicineStock(@RequestBody MmsMedicineStockUpdateReq updateReq, HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return medicineStockService.updateMedicineStock(updateReq, userId);
    }

    @ApiModelProperty("增加库存")
    @PostMapping("/increase")
    public ResponseResult increaseMedicineStock(@RequestBody MmsMedicineStockIncreaseReq increaseReq,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return medicineStockService.increaseMedicineStock(increaseReq,userId);
    }

    @ApiModelProperty("减少库存")
    @PostMapping("/reduce")
    public ResponseResult reduceMedicineStock(@RequestBody MmsMedicineStockReduceReq reduceReq,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return medicineStockService.reduceMedicineStock(reduceReq,userId);
    }


    @ApiModelProperty("删除库存")
    @PostMapping("/deleted")
    public ResponseResult deletedMedicineStock(@RequestBody MmsMedicineStockDeletedReq deletedReq){
        return medicineStockService.deletedMedicineStock(deletedReq);
    }


    @ApiModelProperty("查看库存详情")
    @GetMapping("/id")
    public ResponseResult getMedicineStockDetail(@RequestParam String id){
        return medicineStockService.getMedicineStockDetail(id);
    }


    @ApiModelProperty("查看库存列表")
    @GetMapping("/list")
    public ResponseResult getMedicineStockList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "searchStr",required = false) String searchStr,
                                               @RequestParam(value = "startDate",required = false) String startDate,
                                               @RequestParam(value = "endDate",required = false) String endDate){
        return medicineStockService.getMedicineStockList(pageNum,pageSize,searchStr,startDate,endDate);
    }

    @ApiModelProperty("查看库存历史记录列表")
    @GetMapping("/historyList")
    public ResponseResult getMedicineStockHistoryList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "searchStr", required = false) String searchStr,
                                                      @RequestParam(value = "startDate", required = false) String startDate,
                                                      @RequestParam(value = "stockType", required = false) Integer stockType,
                                                      @RequestParam(value = "endDate", required = false) String endDate,
                                                      @RequestParam(value = "stockId", required = false) String stockId) {

        return medicineStockService.getMedicineStockHistoryList(pageNum,pageSize,searchStr,startDate,endDate,stockType, stockId);
    }

    @ApiOperation("库存导出模版")
    @PostMapping("/batch/export")
    public ResponseResult exportMedicineStock(@RequestBody MmsMedicineStockExportReq medicineStockExportReq,
                                         HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = medicineStockService.exportMedicineStock(medicineStockExportReq, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "库存批量导入")
    @PostMapping("/batch/add")
    public ResponseResult batchMedicineStock(@RequestBody Map<String,Object> map, HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = medicineStockService.batchMedicineStock(map,user_id);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiOperation("库存历史批量导出")
    @PostMapping("/batch/historyExport")
    public ResponseResult exportMedicineStockLog(@RequestBody MmsMedicineStockLogExportReq mmsMedicineStockLogExportReq,
                                         HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = medicineStockService.exportMedicineStockLog(mmsMedicineStockLogExportReq, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

}

